package com.qf.controller;

import com.qf.service.PublishService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/jgpy/publish")
public class PublishController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doPost(req, resp);
        System.out.println("您点击了发布按钮。。。。");
        // 服务器在接收请求的时候 - 需要判断是你什么的请求
        //【1】 统一设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //【2】设置响应的类型
        resp.setContentType("text/html; charset=utf-8");
        //【3】设置Response的响应体：作用-会给数据给前端请求 - 获取字符输出流
        PrintWriter writer = resp.getWriter();
        // -------------------------------------
        String aciton = req.getParameter("action");
        switch (aciton){
            case "发布":
                System.out.println("您选择了发布功能");
                // 调用服务层 - 和 Dao层进行存储图片和数据
                String result = new PublishService().Released(req);
                writer.write(result);
                writer.flush();
                writer.close();
                break;
            case "查询所有":
                String result_all = new PublishService().queryPublist(req);
                writer.write(result_all);
                writer.flush();
                writer.close();
                break;
            case "查询个人":
                String result_sign = new PublishService().queryPublist(req);
                writer.write(result_sign);
                writer.flush();
                writer.close();
                break;
            case "删除":
                String sign_d = req.getParameter("sign");
                String result_d = new PublishService().delete_publish(sign_d);
                writer.write(result_d);
                writer.flush();
                writer.close();
                break;
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doGet(req, resp);
        this.doPost(req,resp);
    }
}
